package com.tanhua.server.service;

import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.model.db.*;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVoAge;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通用设置管理业务处理层
 */
@Service
@Slf4j
public class SettingsService {

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private UserInfoApi userInfoApi;


    /**
     * 用户通用设置 - 读取
     */
    public SettingsVo findSettings() {
        SettingsVo vo = new SettingsVo();
        //1.获取用户数据
        User user = UserHolder.getUser();
        Long userId = user.getId();//当前用户id
        //2.获取陌生人问题（根据当前用户id查询tb_question表得到问题内容）
        Question question = questionApi.findQuestionById(userId);
        //2.1 为了需求给设置默认值
        String txt = "约吗?";//默认值
        if (question != null) {
            txt = question.getTxt();
        }
        //3.获取通知设置数据（根据当前用户id查询tb_settings表得到设置内容）
        Settings settings = settingsApi.findSettingsById(userId);
        if (settings != null) {//默认所有通知是开启的
            BeanUtils.copyProperties(settings, vo);
        }
        vo.setStrangerQuestion(txt);//陌生人问题
        vo.setId(userId);//用户id
        vo.setPhone(user.getMobile());//手机号
        return vo;
    }

    /**
     * 设置陌生人问题 - 保存
     */
    public void addQuestions(String content) {
        Question question = new Question();
        question.setTxt(content);//陌生人问题内容
        question.setUserId(UserHolder.getUserId());//当前用户id
        questionApi.add(question);
    }

    /**
     * 通知设置 - 保存
     */
    public void addSettings(boolean likeNotification, boolean pinglunNotification, boolean gonggaoNotification) {
        Settings settings = new Settings();
        settings.setUserId(UserHolder.getUserId());//当前用户id
        settings.setLikeNotification(likeNotification);//推送喜欢通知
        settings.setPinglunNotification(pinglunNotification);//推送评论通知
        settings.setGonggaoNotification(gonggaoNotification);//推送公告通知
        settingsApi.add(settings);
    }

    /**
     * 黑名单 - 翻页列表
     */
    public PageResult<UserInfoVoAge> findPageBlackList(Long page, Long pagesize) {
        //1.分页查询黑名单分页列表数据
        PageResult<BlackList>  pageBlackList = blackListApi.findPageBlackList(page, pagesize, UserHolder.getUserId());
        if(pageBlackList == null || CollectionUtils.isEmpty(pageBlackList.getItems())){
            return null;//理论情况return null
        }
        //2.从第一步中获取所有黑名单用户ids
        //方式一：
        /*List<BlackList> blackLists = pageBlackList.getItems();
        List<Long> listBlackUserId = new ArrayList<>();
        for (BlackList blackList : blackLists) {
            Long blackUserId = blackList.getBlackUserId();
            listBlackUserId.add(blackUserId);
        }*/
        //方式二：stream方式  BlackList::getBlackUserId(获取什么数据) Collectors.toList()变为list集合
        List<Long> listBlackUserId = pageBlackList.getItems().stream().map(BlackList::getBlackUserId).collect(Collectors.toList());

        //3.根据第二步的所有黑名单用户ids查询用户详情
        //select * from tb_userinfo where id in(1,2,3,4,.......)
        List<UserInfo> userInfoList = userInfoApi.findByBatchIds(listBlackUserId);
        //3.1 将List<UserInfo> 转为 List<UserInfoVoAge>
        List<UserInfoVoAge> infoVoAgeList = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            UserInfoVoAge voAge = new UserInfoVoAge();
            BeanUtils.copyProperties(userInfo,voAge);//头像 昵称 性别 年龄
            voAge.setId(userInfo.getId());//黑名单用户id
            infoVoAgeList.add(voAge);
        }
        //4.将PageResult<BlackList> 转为 PageResult<UserInfoVoAge>
        return new PageResult<>(pageBlackList.getPage(),pageBlackList.getPagesize(),pageBlackList.getCounts(),infoVoAgeList);
    }

    /**
     * 移除黑名单
     * @param blackUserId
     */
    public void deleteBlackList(Long blackUserId) {
        blackListApi.deleteBlackList(blackUserId,UserHolder.getUserId());
    }
}
